home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / devices and hardware / disks / drivergestalt demo / drivergestaltdemo.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  8.6 KB  |  347 lines

  1. /*
  2.     File:        DriverGestaltDemo.c
  3.     
  4.     Description:This sample is a simple example of how to make some basic Driver
  5.                 Gestalt queries.
  6.  
  7.     Author:        VM
  8.  
  9.     Copyright:     Copyright: © 1996-1999 by Apple Computer, Inc.
  10.                 all rights reserved.
  11.     
  12.     Disclaimer:    You may incorporate this sample code into your applications without
  13.                 restriction, though the sample code has been provided "AS IS" and the
  14.                 responsibility for its operation is 100% yours.  However, what you are
  15.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  16.                 after having made changes. If you're going to re-distribute the source,
  17.                 we require that you make it clear in the source that the code was
  18.                 descended from Apple Sample Code, but that you've made changes.
  19.     
  20.     Change History (most recent first):
  21.                 6/23/99    Updated for Metrowerks Codewarrior Pro 2.1
  22.  
  23. */
  24.  
  25.  
  26.  
  27. //------------------------------------------------------------------------------
  28.  
  29. #pragma mark Includes
  30. //------------------------------------------------------------------------------
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34.  
  35. #include <DriverGestalt.h>
  36. #include <Devices.h>
  37. #include <LowMem.h>
  38. #include <Files.h>
  39.  
  40. //------------------------------------------------------------------------------
  41.  
  42. #pragma mark Defines
  43. //------------------------------------------------------------------------------
  44.  
  45.  
  46. #define CLEAR(what) do {                        \
  47.         register Ptr        _ptr = (Ptr) &what;    \
  48.         register Size        _len = sizeof what;    \
  49.         for (; _len > 0; --_len)                \
  50.             *_ptr++ = 0;                        \
  51.     } while (0)
  52.  
  53.  
  54. //------------------------------------------------------------------------------
  55.  
  56. #pragma mark DriverGestalt Info & Structures
  57. //------------------------------------------------------------------------------
  58.  
  59.  
  60. //enum {
  61. //    kdgFlush                    = 'flus'    /*can disk driver flush */
  62. //};
  63.  
  64. //struct DriverGestaltFlushResponse {
  65. //    Boolean                            canFlush;
  66. //    Boolean                            needsFlush;
  67. //    UInt8                            pad[2];
  68. //};
  69. //typedef struct DriverGestaltFlushResponse DriverGestaltFlushResponse;
  70.  
  71. //typedef struct DriverGestaltSyncResponse DriverGestaltSyncResponse;
  72.  
  73. struct DriverGestaltVersResponse
  74. {
  75.     NumVersion driverVersion;
  76. };
  77. typedef struct DriverGestaltVersResponse DriverGestaltVersResponse;
  78.  
  79.  
  80. union DriverGestaltInfo
  81. {
  82.     DriverGestaltSyncResponse        sync;
  83.     DriverGestaltBootResponse        boot;
  84.     DriverGestaltDevTResponse        devt;
  85.     DriverGestaltIntfResponse        intf;
  86.     DriverGestaltVersResponse        vers;
  87.     DriverGestaltEjectResponse        ejec;
  88.     DriverGestaltPowerResponse        powr;
  89.     DriverGestaltFlushResponse        flus;
  90.     UInt32                            i;
  91. };
  92. typedef union DriverGestaltInfo DriverGestaltInfo;
  93.  
  94. //------------------------------------------------------------------------------
  95.  
  96. #pragma mark Prototypes
  97. //------------------------------------------------------------------------------
  98.  
  99.  
  100.  
  101. void         DisplayAllDriveInfo (void);
  102.  
  103. void         DisplayDriverGestaltInfo    (short refNum, short device);
  104.  
  105. OSErr         DoDriverGestalt        (    short driverRefnum,
  106.                                     short driveNumber,
  107.                                     OSType gestaltSelector,
  108.                                     DriverGestaltInfo    *response );
  109.  
  110. char*        DrvrRefToName        (short refNum);
  111. void         PrintNumVersion        (char *label, NumVersion version );
  112.  
  113.  
  114. // ---------------------------------------------------------------------------
  115. void     main     (void)
  116. // ---------------------------------------------------------------------------
  117. {
  118.     DisplayAllDriveInfo();
  119. }
  120.  
  121.  
  122. // ---------------------------------------------------------------------------
  123. void         DisplayAllDriveInfo (void)
  124. // ---------------------------------------------------------------------------
  125. //
  126. //  Display information ATA Driver GestaltInfo
  127. //
  128. {
  129.     register DrvQEl        *drvQElPtr;
  130.     char*                drvrNamePtr;
  131.     short                notused;
  132.     OSErr                status;
  133.  
  134.  
  135. #define DQEL    (*drvQElPtr)
  136.     drvQElPtr = (DrvQEl *) GetDrvQHdr()->qHead;
  137.  
  138.     printf("Disk Drive Queue Information:\n\n");
  139.     while (drvQElPtr != NULL) {
  140.  
  141.         drvrNamePtr = DrvrRefToName(DQEL.dQRefNum);
  142.  
  143.         printf("Drive: %d  %#s (%d) ",
  144.                 (int) DQEL.dQDrive,
  145. drvrNamePtr,DQEL.dQRefNum);
  146.  
  147.         switch (DQEL.qType) {
  148.         case 0:
  149.             printf(
  150.                 "%lu blocks (small drive)\n",
  151.                 (unsigned long) DQEL.dQDrvSz
  152.             );
  153.             break;
  154.         case 1:
  155.             printf(
  156.                 "%lu blocks\n",
  157.                 ((unsigned long) DQEL.dQDrvSz)
  158.                 + (((unsigned long) DQEL.dQDrvSz2) * 0x10000L)
  159.             );
  160.             break;
  161.         case 3:
  162.             printf(
  163.                 "%lu blocks (MFS present)\n",
  164.                 ((unsigned long) DQEL.dQDrvSz)
  165.             );
  166.             break;
  167.         default:
  168.             printf(
  169.                 "%lu %lu (unknown queue type)\n",
  170.                 ((unsigned long) DQEL.dQDrvSz),
  171.                 ((unsigned long) DQEL.dQDrvSz2)
  172.             );
  173.             break;
  174.         }
  175.  
  176. // Open driver
  177.         status = OpenDriver((ConstStr255Param)drvrNamePtr, ¬used);
  178.         if (status != noErr) {
  179.             printf("Cannot open %#s Error: %d\n", (int) status);
  180.         exit(EXIT_FAILURE);
  181.         }
  182.  
  183.         DisplayDriverGestaltInfo(DQEL.dQRefNum, DQEL.dQDrive);
  184.         drvQElPtr = (DrvQEl *) DQEL.qLink;
  185.     }
  186. #undef DQEL
  187. }
  188.  
  189.  
  190.  
  191.  
  192. // ---------------------------------------------------------------------------
  193. void         DisplayDriverGestaltInfo    (short refNum, short driveNum)
  194. // ---------------------------------------------------------------------------
  195. //
  196. //  Display DriverGestalt information.
  197. //
  198. {
  199.         OSErr                status;
  200.         DriverGestaltInfo    response;
  201.  
  202. // display driver version
  203.         status = DoDriverGestalt(refNum,driveNum, kdgVersion,
  204. &response);
  205.         if (status == noErr)
  206.             PrintNumVersion("\t'vers' - version:",
  207. response.vers.driverVersion);
  208.  
  209.  
  210. // display device type
  211.         status = DoDriverGestalt(refNum, driveNum, kdgDeviceType,
  212. &response);
  213.         if (status == noErr)
  214.             printf("\t'devt' - Device:\t'%.4s'\n",
  215.                 (char *) &response.devt.deviceType
  216.             );
  217.  
  218.  
  219. // display API info
  220.         status = DoDriverGestalt(refNum, driveNum, kdgInterface,
  221. &response);
  222.         if (status == noErr)
  223.             printf("\t'intf' - Interface:\t'%.4s'\n",
  224.                 (char *) &response.intf.interfaceType
  225.             );
  226.  
  227. // display boot record
  228.         status = DoDriverGestalt(refNum, driveNum, kdgBoot, &response);
  229.         if (status == noErr)
  230.             printf("\t'boot' - Boot PRAM:\t%08lx\n",
  231.                 (* ((unsigned long *) &response))
  232.             );
  233.  
  234. // display sync info
  235.         status = DoDriverGestalt(refNum, driveNum,  kdgSync,
  236. &response);
  237.         if (status == noErr)
  238.                     printf("\t'sync' - Driver is %s \n",
  239.  
  240.     (response.sync.behavesSynchronously)?"synchronous":"asynchronous");
  241.  
  242.  
  243. // display driver flush capablities
  244.         status = DoDriverGestalt(refNum, driveNum, kdgFlush, &response);
  245.         if (status == noErr)
  246.             printf("\t'flus' - Driver %s flush \n",
  247.  
  248.     (response.flus.canFlush)?"supports":"doesn't support");
  249.  
  250. // display power info
  251.         status = DoDriverGestalt(refNum, driveNum,kdgSupportsPowerCtl, &response);
  252.         if (status == noErr)  {
  253.             printf("\t'psup' - Driver %s power switching\n",
  254.                             (response.powr.powerValue)?"supports":"doesn't support");
  255.  
  256.             status = DoDriverGestalt(refNum, driveNum, kdgMin3VPower,&response);
  257.             if (status == noErr)
  258.                 printf("\t'pmx3' - 3.3V power consumption %d microWatts \n",
  259.                     (char *) &response.powr.powerValue);
  260.     }
  261.  
  262.  
  263.     printf("\n");
  264. }
  265.  
  266.  
  267. // ---------------------------------------------------------------------------
  268. OSErr         DoDriverGestalt        (short
  269.     driverRefnum,
  270.                                  short
  271.             driveNumber,
  272.                                 OSType
  273.             gestaltSelector,
  274.  
  275.     DriverGestaltInfo    *response )
  276. // ---------------------------------------------------------------------------
  277. //
  278. //  Do Driver Gestalt Call
  279. //
  280. {
  281.         DriverGestaltParam        pb;
  282.         OSErr                    status;
  283.  
  284.         CLEAR(pb);
  285.         pb.ioVRefNum     = driveNumber;
  286.         pb.ioCRefNum    = driverRefnum;
  287.         pb.csCode         = kDriverGestaltCode;
  288.         pb.driverGestaltSelector = gestaltSelector;
  289.  
  290.         status = PBStatusSync((ParmBlkPtr) &pb);
  291.  
  292.         if (status == noErr)
  293.             response->i  = pb.driverGestaltResponse;
  294. //        else printf("error %d\n",status);
  295.         return (status);
  296. }
  297.  
  298. // ---------------------------------------------------------------------------
  299. char*        DrvrRefToName(short refNum)
  300. // ---------------------------------------------------------------------------
  301. //
  302. //  lookup driver name in table
  303. //
  304.  
  305. {
  306.         AuxDCEHandle*        UTable  = (AuxDCEHandle*)
  307.         LMGetUTableBase();
  308.         DCtlPtr                dctl;
  309.         Ptr                    p;
  310.  
  311.         if(!refNum) return ((char*) "\p<none>");
  312.  
  313.         dctl = (DCtlPtr) *UTable[~refNum];
  314.         p      =  dctl->dCtlDriver;
  315.         if( dctl->dCtlFlags  & 0x0040) p = (void*) *p;
  316.  
  317.         return  ( p?(char*) (p+18):(char*)"\p-Purged-");
  318. }
  319.  
  320. // ---------------------------------------------------------------------------
  321. void         PrintNumVersion        (char *label, NumVersion version )
  322. // ---------------------------------------------------------------------------
  323. //
  324. //    Decode version number info
  325. //
  326. {
  327.     char            *stage;
  328.  
  329.     switch (version.stage) {
  330.     case developStage:    stage = "d";        break;
  331.     case alphaStage:    stage = "a";        break;
  332.     case betaStage:        stage = "b";        break;
  333.     case finalStage:    stage = "";            break;
  334.     default:            stage = "?";        break;
  335.  
  336.     }
  337.     printf("%s %d.%d.%d",
  338.         label,
  339.         version.majorRev,
  340.         (version.minorAndBugRev>>4), (version.minorAndBugRev & 0xf),
  341.         stage);
  342.     if(version.stage != finalStage) printf(".%d",version.nonRelRev);
  343.  
  344.     printf(" (hex %08lx)\n",    (* ((unsigned long *) &version)));
  345. }
  346.  
  347.